<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>父组件访问子组件</title>
</head>
<body>
<div id="app">
    <cpn1 id="cpn1" ref="cpn1"></cpn1>
    <cpn2 id="cpn2" ref="cpn2"></cpn2>

    <button v-on:click="getComponents">获取组件</button>
</div>

<template id="cpn">
    <h1>Hello, World!</h1>
</template>

<script type="module">
    import {createApp, ref} from "../../../../js/vue.esm-browser.js";
    import {defineComponent} from "../../../../js/vue.esm-browser.js";

    let cpn1 = defineComponent(
        {
            template: "#cpn",
            setup() {
                let name = ref("这是组件 cpn1 中的 name")
                function showName() {
                    console.log("调用了组件 cpn1 中的 showName 函数");
                }

                return {
                    name,
                    showName,
                };
            }
        }
    );

    let cpn2 = defineComponent(
        {
            template: "#cpn",
        }
    );
    let app = createApp(
        {
            setup() {

                let cpn1 = ref(null)

                function getComponents() {
                    /*// 通过传统 JS 的方式获取的是 对应的 HTML 代码
                    let cpn1 = document.getElementById("cpn1");
                    console.log(cpn1);*/

                    // 通过 ref 获得的整个组件内容
                    console.log(cpn1);
                    console.log(cpn1.value);
                    console.log(cpn1.value.name);
                    cpn1.value.showName()

                }

                return {
                    getComponents,
                    cpn1,
                }
            },
            components: {
                cpn1,
                cpn2,
            }
        }
    )
    app.mount("#app")
</script>
</body>
</html>